package com.winit.openapi.cwm.csi.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.cwm.spi.csi.DeductionService;
import com.winit.cwm.spi.csi.InboundOrderService;
import com.winit.cwm.spi.csi.ScanOperationService;
import com.winit.cwm.spi.csi.command.OperationRecordCommand;
import com.winit.cwm.spi.csi.command.PageOperationRecordCommand;
import com.winit.cwm.spi.csi.command.ScanOperationCommand;
import com.winit.cwm.spi.csi.command.ScanPageInboundOrderCommand;
import com.winit.cwm.spi.csi.command.deduction.PageDeductionOrderCommand;
import com.winit.cwm.spi.csi.vo.OperationRecordVo;
import com.winit.cwm.spi.csi.vo.ScanInboundOrderResponseVo;
import com.winit.cwm.spi.csi.vo.ScanPageDeductionOrderVo;
import com.winit.cwm.spi.csi.vo.ScanResponseVo;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.customer.QueryCompanylCommand;
import com.winit.ums.spi.vo.CustomerInfoVO;

@Controller
@RequestMapping(value = "/csi/scan")
public class ScanOperationController extends BaseController {

    @Resource
    private ScanOperationService scanOperationService;

    @Resource(name = "csiInboundOrderService")
    private InboundOrderService inboundOrderService;

    @Resource
    private DeductionService deductionService;

    /**
     * ums客户信息
     */
    @Resource
    private UmsCustomerService   umsCustomerService;

    /**
     * 扫描枪操作方法
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/scanOperation")
    public String scanOperation(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        ScanOperationCommand command = JSON.parseObject(obj.toString(), ScanOperationCommand.class);
        command.setData(obj);
        command.setCtx(CommandContext.getContext());
        command.setCustomerCode(getContextCustomerInfo().getCode());

        ScanResponseVo scanResponseVo = scanOperationService.sacnOperation(command);
        responseMsg.setData(scanResponseVo);
        return SUCCESS;
    }

    /**
     * 扫描枪操作方法
     * 
     * @param requestMsg 返回
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/goBack")
    public String goBack(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        ScanOperationCommand command = JSON.parseObject(obj.toString(), ScanOperationCommand.class);
        command.setData(obj);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        ScanResponseVo scanResponseVo = scanOperationService.goBack(command);
        responseMsg.setData(scanResponseVo);
        return SUCCESS;
    }

    /**
     * 分页查询订单列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageInboundOrder")
    public String pageOverseaInventories(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        ScanPageInboundOrderCommand command = JSON.parseObject(obj.toString(), ScanPageInboundOrderCommand.class);
        command.setData(obj);
        command.setCtx(CommandContext.getContext());

        command.setCustomerCode(getContextCustomerInfo().getCode());

        ScanInboundOrderResponseVo response = scanOperationService.pageOrders(command);
        responseMsg.setData(response);
        return SUCCESS;
    }

    /**
     * 分页查询扣货订单列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageDeductionOrders")
    public String scanPageDeductionOrders(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PageDeductionOrderCommand command = JSON.parseObject(requestMsg.getData().toString(), PageDeductionOrderCommand.class);
        command.setCustomerCode(this.getContextCustomerInfo().getCode());
        ScanPageDeductionOrderVo scanPageDeductionOrderVo = deductionService.scanPageDeductionOrder(command);
        responseMsg.setData(scanPageDeductionOrderVo);
        return SUCCESS;
    }

    /**
     * 分页查询操作记录
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageOperationRecord")
    public String pageOperationRecord(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PageOperationRecordCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageOperationRecordCommand.class);
        command.setCustomerCode(getContextCustomerInfo().getCode());

        Page<OperationRecordVo> response = scanOperationService.pageOperationRecords(command);
        responseMsg.setData(response);
        return SUCCESS;
    }

    /**
     * 查询最后一次操作记录
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryLastOperationRecord")
    public String queryLastOperationRecord(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {
        OperationRecordCommand command = JSON.parseObject(requestMsg.getData().toString(), OperationRecordCommand.class);
        command.setCustomerCode(getContextCustomerInfo().getCode());
        ScanResponseVo response = scanOperationService.queryLastOperationRecord(command);
        responseMsg.setData(response);
        return SUCCESS;
    }

    /** 从上下文中获取客户信息 */
    private CustomerInfoVO getContextCustomerInfo() {
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        return customer;
    }
}
